package com.police.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.police.R;
import com.police.model.JiZhanListBean;

import java.util.List;

public class JiZhanListAdapter extends BaseAdapter {
    private Context mContext;
    private List<JiZhanListBean> jiZhanList;
    private String zimuStr[];
    private OnDataItemDeleted onDataItemDeleted;

    public JiZhanListAdapter(Context mContext, List<JiZhanListBean> jiZhanList) {
        this.mContext = mContext;
        this.jiZhanList = jiZhanList;
        zimuStr = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
                "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return jiZhanList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return jiZhanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            LayoutInflater layoutInflater = ((Activity) mContext).getLayoutInflater();
            convertView = layoutInflater.inflate(R.layout.jizhan_listview_item, parent, false);
            convertView.setTag(convertView);
        } else {
            convertView = (View) convertView.getTag();
        }
        TextView idTextView = (TextView) convertView.findViewById(R.id.idd);
        idTextView.setText(zimuStr[position]);
        TextView lacValueText = (TextView) convertView.findViewById(R.id.lacValue);
        lacValueText.setText(jiZhanList.get(position).getLac());
        TextView cidValue = (TextView) convertView.findViewById(R.id.cidValue);
        cidValue.setText(jiZhanList.get(position).getCellid());
        TextView yysName = (TextView) convertView.findViewById(R.id.yysName);
        if (jiZhanList.get(position).getMnc().equals("0")) {
            yysName.setText("移动");
        } else if (jiZhanList.get(position).getMnc().equals("1")) {
            yysName.setText("联通");
        } else if (jiZhanList.get(position).getMnc().equals("2")) {
            yysName.setText("电信");
        }

        TextView delete = (TextView) convertView.findViewById(R.id.delete);
        delete.setTag(position);
        delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int position = Integer.parseInt(v.getTag().toString());

                jiZhanList.remove(position);
                if (onDataItemDeleted != null) {
                    onDataItemDeleted.notifyDataItemDeleted(position);
                }
                JiZhanListAdapter.this.notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public void setOnDataItemDeleted(OnDataItemDeleted onDataItemDeleted) {
        this.onDataItemDeleted = onDataItemDeleted;
    }

    public static interface OnDataItemDeleted {
        public void notifyDataItemDeleted(int pos);
    }

}
